using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using DG.Tweening;
using AppEventUtils;
using AppSoldierState;
using UnityEngine.Serialization;


public class MyPointsManager: MonoBehaviour
{
    public Transform player;
    public Transform enemy;
    public Transform store_owner;
    public Transform skill;
    public Transform refresh;
    public Transform freeze;
    public Transform level_up;
    
    [HideInInspector] public CardController player_comp;
    [HideInInspector] public CardController enemy_comp;
    [HideInInspector] public CardController store_owner_comp;

    [HideInInspector] public Vector3 player_word_pos;
    [HideInInspector] public Vector3 store_owner_word_pos;
    [HideInInspector] public Vector3 enemy_word_pos;
    [HideInInspector] public Vector3 skill_word_pos;
    [HideInInspector] public Vector3 refresh_word_pos;
    [HideInInspector] public Vector3 freeze_word_pos;
    [HideInInspector] public Vector3 level_up_word_pos;

    [HideInInspector] public bool is_ready = false;

    [FormerlySerializedAs("test_trans")] public Transform test_trans__player;
    public Transform test_trans__store_owner;
    public Transform test_trans__enemy;
    public Transform test_trans__skill;
    public Transform test_trans__level_up;

    public static MyPointsManager instance;

    void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    public GameObject stars;
    public void add_store_owner(bool cancel = false)
    {
        if (cancel)
        {
            GameController.instance.store_level_stars.SetActive(false);
            test_trans__store_owner.gameObject.SetActive(false);
            // ObjectPool.instance.RecycleObj(test_trans__store_owner.gameObject);
            return;
        }

        if (!is_ready)
        {
            var obj = GameController.instance.core_load_card_obj_by_id(2, role_type: RoleTypeEnum.None, is_in_hand: false);
            test_trans__store_owner = obj.transform;
            test_trans__store_owner.position = store_owner_word_pos;
            test_trans__store_owner.gameObject.SetActive(false);
            GameController.instance.store_level_stars.SetActive(false);
            
            store_owner_comp = test_trans__store_owner.GetComponent<CardController>();
            
            // BdPackage.BdUtils.log("--- add_store_owner 1");
        }
        else
        {
            test_trans__store_owner.gameObject.SetActive(true);
            GameController.instance.store_level_stars.SetActive(true);
            // BdPackage.BdUtils.log("--- add_store_owner 2");
        }

        store_owner_comp.btn_change_to_soldier_state();
        store_owner_comp.add_scale_size_animation_event();
    }
    
    public void add_enemy_hero(bool cancel = false)
    {
        if (cancel)
        {
            GameController.instance.recycle_one_card(test_trans__enemy.gameObject);
            // ObjectPool.instance.RecycleObj(test_trans__enemy.gameObject);
            return;
        }
        var obj = GameController.instance.core_load_card_obj_by_id(3, role_type: RoleTypeEnum.None, is_in_hand: false);
        test_trans__enemy = obj.transform;
        test_trans__enemy.transform.position = enemy_word_pos;
        
        enemy_comp = test_trans__enemy.GetComponent<CardController>();
        enemy_comp.btn_change_to_soldier_state();
        enemy_comp.add_scale_size_animation_event();
    }

    private void Start()
    {
        StartCoroutine(get_word_positions());
    }

    IEnumerator get_word_positions()
    {
        yield return new WaitForSeconds(0.1f);

        player_word_pos = ScreenPositionTools.set_z_position(player.position);
        store_owner_word_pos = ScreenPositionTools.set_z_position(store_owner.position);
        enemy_word_pos = ScreenPositionTools.set_z_position(enemy.position);
        skill_word_pos = ScreenPositionTools.set_z_position(skill.position);
        refresh_word_pos = ScreenPositionTools.set_z_position(refresh.position);
        freeze_word_pos = ScreenPositionTools.set_z_position(freeze.position);
        level_up_word_pos = ScreenPositionTools.set_z_position(level_up.position);
        
        // player_word_pos = isRectTransform(player)? ScreenPositionTools.conv_screen_pos_to_word_pos(player.position): player.position;
        // store_owner_word_pos = isRectTransform(store_owner)? ScreenPositionTools.conv_screen_pos_to_word_pos(store_owner.position): store_owner.position;
        // enemy_word_pos = isRectTransform(store_owner)? ScreenPositionTools.conv_screen_pos_to_word_pos(store_owner.position): store_owner.position;
        // skill_word_pos = isRectTransform(skill)? ScreenPositionTools.conv_screen_pos_to_word_pos(skill.position): skill.position;
        // refresh_word_pos = isRectTransform(refresh)? ScreenPositionTools.conv_screen_pos_to_word_pos(refresh.position): refresh.position;
        // freeze_word_pos = isRectTransform(freeze)? ScreenPositionTools.conv_screen_pos_to_word_pos(freeze.position): freeze.position;
        // level_up_word_pos = isRectTransform(level_up)? ScreenPositionTools.conv_screen_pos_to_word_pos(level_up.position): level_up.position;

        add_store_owner();
        
        var obj = GameController.instance.core_load_card_obj_by_id(1, role_type: RoleTypeEnum.None, is_in_hand: false);
        
        test_trans__player = obj.transform;
        player_comp = test_trans__player.GetComponent<CardController>();
        player_comp.btn_change_to_soldier_state();

        test_trans__player.transform.position = player_word_pos;

        // test_trans__player.DOMove(player_word_pos, 1.5f).OnComplete(() =>
        // {
        //     // enemy_comp = test_trans__enemy.GetComponent<CardController>();
        //     is_ready = true;
        // });
        
        is_ready = true;
    }

    public bool isRectTransform(Transform trans)
    {
        // return trans is RectTransform;
        return false;
    }


}